package com.xxg.renrensearch.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrencommon.domain.bean.ProductGatherInfo;
import com.xxg.renrensearch.domain.result.ShopGatherCount;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 商品采集信息dao
 *
 * @author szj
 * @date 2021/11/29
 */
@Repository
public class ProductGatherInfoDao {
    private final MongoTemplate mongoTemplate;

    public ProductGatherInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "productGatherInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(ProductGatherInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.ascending("userId"));
        collection.createIndex(Indexes.ascending("shopId"));
        collection.createIndex(Indexes.ascending("spuId"));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("spuId"), Indexes.ascending("userId")));
    }

    /**
     * 按照采集店铺、被采集店铺分组统计出采集数量
     *
     * @return 结果
     */
    public List<ShopGatherCount> getAllGatherCount() {
        List<AggregationOperation> operations = new ArrayList<>();
        GroupOperation groupOperation = group("shopId", "originShopId")
                .first("shopId").as("shopId")
                .first("originShopId").as("originShopId")
                .count().as("count");
        operations.add(groupOperation);
        Aggregation agg = newAggregation(operations);
        AggregationResults<ShopGatherCount> results = mongoTemplate.aggregate(agg, ProductGatherInfo.class,
                ShopGatherCount.class);
        return results.getMappedResults();
    }

    /**
     * 根据shopId查询记录
     *
     * @param shopId 店铺ID
     * @return 结果
     */
    public List<ProductGatherInfo> getGatherListByShopId(Integer shopId) {
        Query query = new Query(where("shopId").is(shopId));
        query.fields().include("newSpuId");
        return mongoTemplate.find(query, ProductGatherInfo.class);
    }

    /**
     * 检查是否有采集记录
     *
     * @param newSpuId 采集后的商品ID
     * @return 结果
     */
    public boolean existGatherIinfo(Integer newSpuId) {
        Query query = new Query(where("newSpuId").is(newSpuId));
        return mongoTemplate.exists(query, ProductGatherInfo.class);
    }

    /**
     * 插入采集记录
     *
     * @param productGatherInfo 采集记录
     */
    public void insert(ProductGatherInfo productGatherInfo) {
        productGatherInfo.setCreateTime(new Date());
        productGatherInfo.setUpdateTime(new Date());
        mongoTemplate.insert(productGatherInfo);
    }

    public long countBeGatherBySpuId(Integer spuId) {
        Query query = new Query(where("spuId").is(spuId));
        return mongoTemplate.count(query, ProductGatherInfo.class);
    }
}
